﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpSugar.Extensions
{
	public static class ExtLinq
	{
		/// <summary>
		/// Perform an action on every element
		/// </summary>
		/// <typeparam name="T">The type of data to enumerate</typeparam>
		/// <param name="self">The data to loop through</param>
		/// <param name="action">The action to perform</param>
		/// <returns>The original IEnumerable</returns>
		public static IEnumerable<T> Loop<T>(this IEnumerable<T> self, Action<T> action)
		{
			foreach (T t in self)
				action(t);

			return self;
		}

		/// <summary>
		/// Cache the value of an IEnumerable
		/// </summary>
		/// <typeparam name="T">The type of data to cache</typeparam>
		/// <param name="self">The data to cache</param>
		/// <returns>An IEnumerable that will not be recalculated every time</returns>
		public static IEnumerable<T> Cache<T>(this IEnumerable<T> self)
		{
			return self.ToArray();
		}

		/// <summary>
		/// Join an IEnumerable of elements together
		/// </summary>
		/// <typeparam name="T">The type of data to join</typeparam>
		/// <param name="self">The data to join</param>
		/// <param name="byWhat">What to join the data with</param>
		/// <returns>A single string with all the elements joined together</returns>
		public static String JoinAsStrings<T>(this IEnumerable<T> self, String byWhat)
		{
			return String.Join(byWhat, self);
		}


	}
}
